import MD5 from "js-md5"

// var baseUrl = 'http://test-circ-assets.num.red/api';
// var FileUploadUrl = 'https://upload.num.red'; //文件图片上传域名

import store from '@/store';

if (process.env.NODE_ENV === 'development') { // 测试
	var baseUrl = 'http://test-circ-assets.num.red/api';
	var FileUploadUrl = 'https://upload.num.red'; //文件图片上传域名
	// #ifdef H5
	baseUrl = '/xyr'
	// #endif
} else { // 线上
	var baseUrl = 'https://circ-assets.rryxw.com/api';
	var FileUploadUrl = 'https://upload.num.red'; //文件图片上传域名
}

function getTime() {
	return new Promise((resolve) => {
		uni.request({
			url: baseUrl + '/servicesTime',
			method: 'GET',
			success: res => {
				resolve(res.data)
			}
		})
	})
}
// const appid = "a030250a8d3062d8e6b550f84478b3c7"
// const key = "aac18ae9b6d1ee0d09642535b5a9e25c"
const httpRequest = async (url, method, data) => {

	let slol = {}
	// let t = await getTime(); // = new Date().getTime(); //时间戳
	let t = await new Date().getTime(); // = new Date().getTime(); //时间戳
	let code = '' //六位随机数
	for (var i = 0; i < 6; i++) {
		code += parseInt(Math.random() * 10)
	}
	slol.timeStamp = t;
	// slol.secretKeyWord = key;
	slol.route = url;
	slol.random = code;
	// slol.appId = key;
	let obj = {}
	// 把对象键按字母升序；
	let news = Object.keys(slol).sort();
	// 遍历数组从新组合成对象；
	for (var i = 0; i < news.length; i++) {
		if (typeof slol[news[i]] == 'number') {
			slol[news[i]] = slol[news[i]].toString()
		}
		obj[news[i]] = slol[news[i]]; //向新创建的对象中按照排好的顺序依次增加键值对
	}

	let newobj = JSON.stringify(obj)
	let signature = MD5(newobj);
	let httpDefaultOpts = {
		url: baseUrl + url,
		data: data,
		method: method,
		header: method == 'get' ? {
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
			'Content-Type': 'application/json;',
			// 'Content-Type': 'application/json; charset=UTF-8',
			// "Content-Type": "application/x-www-form-urlencoded",
			// "timeStamp": t,
			// // "appId": appid,
			// "signature": signature,
			// "route": url,
			// "token": '',
			// "random": code
		} : {
			'X-Requested-With': 'XMLHttpRequest',
			'Content-Type': 'application/json;',
			// 'Content-Type': 'application/x-www-form-urlencoded',
			// 'Content-Type': 'text/html; charset=UTF-8',
			// "timeStamp": t,
			// // "appId": appid,
			// "signature": signature,
			// "route": url,
			// "token": '',
			// "random": code
		},
		dataType: 'json',
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				resolve(res[1].data)
			}
		).catch(
			(response) => {
				uni.hideLoading();
				reject(response)
			}
		)
	})
	return promise

};
//带Token请求
const httpTokenRequest = async (url, method, data) => {
	let slol = {}
	// let t = await getTime(); //new Date().getTime(); //时间戳
	let t = await new Date().getTime(); // = new Date().getTime(); //时间戳
	let code = '' //六位随机数
	for (var i = 0; i < 6; i++) {
		code += parseInt(Math.random() * 10)
	}
	slol.timeStamp = t;
	// slol.secretKeyWord = key;
	slol.route = url;
	slol.random = code;
	let obj = {}
	// 把对象键按字母升序；
	let news = Object.keys(slol).sort();
	// 遍历数组从新组合成对象；
	for (var i = 0; i < news.length; i++) {
		if (typeof slol[news[i]] == 'number') {
			slol[news[i]] = slol[news[i]].toString()
		}
		obj[news[i]] = slol[news[i]]; //向新创建的对象中按照排好的顺序依次增加键值对
	}
	let newobj = JSON.stringify(obj)
	let signature = MD5(newobj);
	// 同步获取token 
	//uni.setStorageSync("token",'cO7H246A9aWvRheVP8Ga8aQtyc1cdfrH')
	let lifeData = uni.getStorageSync('lifeData');
	//此token是登录成功后后台返回保存在storage中的
	let httpDefaultOpts = {
		url: baseUrl + url,
		data: data,
		method: method,
		header: method == 'get' ? {
			'token': lifeData.token || '',
			// 'token':'fe13b5eefc9e4de27ef2a9d06d73deb9',
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
			'Content-Type': 'application/json;',
			// "Content-Type": "application/x-www-form-urlencoded",
			// "timeStamp": t,
			// // "appId": appid,
			// "signature": signature,
			// "route": url,
			// "random": code
		} : {
			'token': lifeData.token || '',
			// 'token':'fe13b5eefc9e4de27ef2a9d06d73deb9',
			'X-Requested-With': 'XMLHttpRequest',
			'Content-Type': 'application/json;',
			// "Content-Type": "application/x-www-form-urlencoded",
			// "timeStamp": t,
			// // "appId": appid,
			// "signature": signature,
			// "route": url,
			// "random": code

		},
		dataType: 'json',
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				// 判断是否未登录
				if (res[1].data.msg == '请重新登录' && res[1].data.code == 401) {
					store.commit('clearStauts');
					uni.removeStorageSync('lifeData')
					uni.showToast({
						icon: 'none',
						title: res[1].data.msg,
						duration: 1500
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login',
						});
					}, 800)
				} else {
					resolve(res[1].data)
				}

			}
		).catch((response) => {
			uni.hideLoading();
			reject(response)
		})
	})
	return promise
};

export default {
	FileUploadUrl,
	baseUrl,
	httpRequest,
	httpTokenRequest
}